package com.tust.app.controller;

import com.tust.app.domain.TUser;
import com.tust.app.service.TUserService;
import com.tust.app.utils.MD5Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller

public class AdminController {
    @Resource
    private TUserService  userService;
    @RequestMapping("/adminLogin")
    public  String  adLogin(String username, String password, HttpServletRequest request){
        password = MD5Utils.md5PlusSalt(password);
        TUser u = userService.checkreg(username);
        if (u != null) {
            if (u.getPassword().equals(password)) {
                request.setAttribute("username", username);
                HttpSession session = request.getSession();
                session.setAttribute("username", u.getUsername());
                //检查用户组群
                if(u.getRoleId()==1){
                    return"admin/home";
                }else{
                    return"forward:owner";
                }
            }else {
                return "adlogin";
            }
        }

        return "register";
    }
    @RequestMapping("/checkLogin")
    public String checkLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        if (username != null) {
            TUser u = userService.checkreg(username);
            if (u != null) {
                //检查用户组群
                if(u.getRoleId()==1){
                    return"admin/home";
                }else{
                    return"forward:owner";
                }
            }
            return "admin/home";
        } else {
            // 用户未登录
            request.setAttribute("error", "请先登录");
            return "adlogin";
        }
    }
}



